<template>
    <div>
        <NavTop></NavTop>
        <div style="width: 1400px; margin: auto;">
            <el-table ref="multipleTableRef" :data="cartData" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column property="id" label="汽车编号" width="120" />
                <el-table-column property="name" label="车辆名称" width="400" />
                <el-table-column property="onlineprice" label="价格" width="120" />
                <el-table-column property="num" label="数量" width="120" />
                <el-table-column property="total" label="总价" width="120" />
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div>合计<h1>￥{{ zong }}</h1>
            </div>
            <div style="margin-top: 20px">
                <el-button @click="buy()" :disabled="!btn">购买</el-button>
            </div>

        </div>
    </div>
</template>
<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import router from '@/router';
import NavTop from '@/components/NavTop.vue';
import { ref } from 'vue'
import { ElTable } from 'element-plus'
import { useGoodsStore } from '@/stores/goods';
const zong = ref(0)
const btn = ref(false)
const store = useGoodsStore()
const open2 = () => {

}
// const {createApp,reactive,toRefs,computed}=Vue
interface User {
    date: string
    name: string
    address: string
}
//从session里面获得购物车商品数据
const cartData = ref(JSON.parse(sessionStorage.getItem("cart")))
//购物车数据删除操作
const handleDelete = (index: number, row: User) => {
    console.log(index);
    
    ElMessageBox.confirm("你是否删除这个商品", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    })
        .then(() => {
            cartData.value.splice(index, 1)
            //把更新后的数据重新放回到session里面
            sessionStorage.setItem("cart", JSON.stringify(cartData.value))
        })
        .catch(() => {
            ElMessage({
                type: "info",
                message: "取消购买",
            });
        });

}

//multpleSelection数据用来放置复选框选择后的数据
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<User[]>([])
const buy = () => {
    console.log(multipleSelection.value);
    
    
    ElMessageBox.confirm(
        '你是否确定购买这个商品',
        '提示',
        {
            confirmButtonText: '购买',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            //创建一个日期对象
            const time = new Date()
            //1.生成订单号
            const orderID = ref(time.getFullYear().toString() +
                time.getMonth().toString() +
                time.getDate().toString() +
                Math.random().toString().slice(2, 10))
            //后台打印验证订单号
            console.log(orderID.value);
            //生成总金额和总数量
            let num = ref(0)//放总数量
            let sum = ref(0)//放总金额
            multipleSelection.value.map(v => {
                num.value += v.num
                sum.value += v.total
            })
            //后台打印验证数据是否正确
            console.log(num.value, sum.value);
            //把商品信息添加到订单数组中去
            store.orderAdd({
                orderID: orderID.value,
                num: num.value,
                sum: sum.value,
                date: time,
                isOrder: false,
                child: [...multipleSelection.value]//...展开运算符，把数组数据展开操作，相当于进行了循环
            })
            //在后台打印orderDate数据，查看是否添加完成
            console.log(store.orderData);
            //跳转到order界面
            router.push('/order')
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消购买',
            })
        })

}
const handleSelectionChange = (val: User[]) => {

    zong.value = 0
    multipleSelection.value = val
    multipleSelection.value.map(v => {
        zong.value += v.total
    })
    if (zong.value > 0) {
        btn.value = true
    } else {
        btn.value = false
    }




}

</script>